/**
 * <p>Title: UserGroupSvcImpl</p>
 * <p>Description:  The implement of UserGroupSvc</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Wan ShuaiShuai & qianpengji
 * @version 1.0
 * <pre>Histroy:
 *       2016-9-18  Wan ShuaiShuai & qianpengji  Create
 *</pre>
*/

package cn.jhg.account.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.jhg.account.entity.AuditTrailObject;
import cn.jhg.account.entity.AuditTrailRecord;
import cn.jhg.account.entity.AuditTrailType;
import cn.jhg.account.entity.Authority;
import cn.jhg.account.entity.User;
import cn.jhg.account.entity.UserGroup;
import cn.jhg.account.entity.UserGroupStatus;
import cn.jhg.account.persist.AuthorityDao;
import cn.jhg.account.persist.UserDao;
import cn.jhg.account.persist.UserGroupDao;
import cn.jhg.account.service.AuditTrailSvc;
import cn.jhg.account.util.SequenceProducer;
import cn.jhg.account.util.ServiceResult;

@Service("userGroupSvc")
@Transactional
public class UserGroupSvcImpl implements UserGroupSvc{
	@Resource
	private UserGroupDao userGroupDao;
	@Resource
	private SequenceProducer sequenceProducer;
	@Resource
	private AuditTrailSvc auditTrailSvc;
	@Resource
	private AuthorityDao authorityDao;
	@Resource
	private UserDao userDao;
	
	private Logger log = LoggerFactory.getLogger(UserGroupSvcImpl.class);
	
	/**
	 * <p>Function: getUserGroupById</p>
	 * <p>Description: 根据id通过dao层获取userGroup对象</p>
	 * @param ErrorCode 错误代码
	 * @param int id 用户组id
	 * @return proxyResult 返回结果封装了Data和ErrorCode
	 */
	@Override
	public ServiceResult<UserGroup> getUserGroupById(int id,int iuid) {
		ServiceResult<UserGroup> proxyResult=new ServiceResult<UserGroup>();
		try {
			UserGroup userGroup=userGroupDao.getUserGroupById(id);
			if(userGroup==null){
				proxyResult.setErrorCode(217663293);//用户组id不存在
				log.error("[ERR217663293] 此id的用户组不存在");
			}else{
				proxyResult.setErrorCode(0);//查询成功
				proxyResult.setData(userGroup);
				log.info("[INF221722448] 获取用户组成功");
			}
			return proxyResult;
		} catch (Exception e) {
			proxyResult.setErrorCode(218085386);
			log.error("[ERR218085386] 通过id查询失败" + e.getMessage());
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}
		
	}

	/**
	 * <p>Function: getUserGroupByName</p>
	 * <p>Description: 根据name通过dao层获取userGroup对象</p>
	 * @param ErrorCode 错误代码
	 * @param String name 用户组name
	 * @return proxyResult 返回结果封装了Data和ErrorCode
	 */
	@Override
	public ServiceResult<UserGroup> getUserGroupByName(String name,int iuid) {
        ServiceResult<UserGroup> proxyResult=new ServiceResult<UserGroup>();
		try {
			UserGroup userGroup=userGroupDao.getUserGroupByName(name);
			if(userGroup==null){
				proxyResult.setErrorCode(217663535);//用户组name不存在
				log.error("[ERR217663535] 此name的用户组不存在");
			}else{
				proxyResult.setErrorCode(0);//查询成功
				proxyResult.setData(userGroup);
				log.info("[INF221722451] 获取用户组成功");
			}
			return proxyResult;
		} catch (Exception e) {
			proxyResult.setErrorCode(218085527);
			log.error("[ERR218085527] 通过name查询失败" + e.getMessage());
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}
		
	}

	/**
	 * <p>Function: getUserGroupByCode</p>
	 * <p>Description: 根据code通过dao层获取userGroup对象</p>
	 * @param ErrorCode 错误代码
	 * @param String code 用户组code
	 * @return proxyResult 返回结果封装了Data和ErrorCode
	 */
	@Override
	public ServiceResult<UserGroup> getUserGroupByCode(String code,int iuid) {
        ServiceResult<UserGroup> proxyResult=new ServiceResult<UserGroup>();
		try {
			UserGroup userGroup=userGroupDao.getUserGroupByCode(code);
			if(userGroup==null){
				proxyResult.setErrorCode(217663598);//用户组code不存在
				log.error("[ERR217663598] 此code的用户组不存在");
			}else{
				proxyResult.setErrorCode(0);//查询成功
				proxyResult.setData(userGroup);
				log.info("[INF221722524] 获取用户组成功");
			}
		    return proxyResult;
		} catch (Exception e) {
			proxyResult.setErrorCode(218085550);
			log.error("[ERR218085550] 通过code查询失败" + e.getMessage());
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}
		
	}
	
	/**
	 * <p>Function: findUserGroupsByName</p>
	 * <p>Description: 根据传入condition值查询用户数据</p>
	 * @author wan shuaishuai
	 * @param   ErrorCode  错误代码
	 * @param   Data       用户组数据
	 * @return  proxyResult   返回结果封装了Data和ErrorCode   
	 */
	@Override
	public ServiceResult<List<Integer>> findUserGroupsByName(String condition,int iuid) {
		ServiceResult<List<Integer>> proxyResult = new ServiceResult<List<Integer>>();
		try{
			String name = "%"+condition+"%";
			List<Integer> set = userGroupDao.findUserGroupsByName(name);
			if(set.isEmpty()){
				proxyResult.setErrorCode(217663252);//用户组名不存在
				log.error("[ERR217663252] 此condition查询的用户组不存在");
			}else{				
				proxyResult.setData(set);
				proxyResult.setErrorCode(0);//查询成功
				log.info("[INF221722554] 通过用户组名查询用户组成功");
			}
			return  proxyResult;
		}catch(Exception e){
			proxyResult.setErrorCode(218085253);
			log.error("[ERR218085253] 通过用户组名查询用户组失败" + e.getMessage() );
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return  proxyResult;
		}
	}
	
	/**
	 * <p>Function: addUserGroup</p>
	 * <p>Description: 添加用户组</p>
	 * @author wan shuaishuai
	 * @param   ErrorCode   错误代码
	 * @param  	int sid 签名 id（操作前用户确认签名记录的 id)
	 * @param   Data        用户组数据
	 * @param   ID			用户组ID值
	 * @return proxyResult	 返回结果封装了Data和ErrorCode   
	 */
	@Override
	public ServiceResult<Integer> addUserGroup(UserGroup group, int sid, int iuid) {
		ServiceResult<Integer> proxyResult = new ServiceResult<Integer>();
		try{
			String name = group.getName();
			String code = group.getCode();
			UserGroup ugByName = userGroupDao.getUserGroupByName(name);
			UserGroup ugByCode = userGroupDao.getUserGroupByCode(code);
			if(ugByName!=null){
				proxyResult.setErrorCode(217760469);//用户组名已存在
				log.error("[ERR217760469] 此name获取的用户组已存在");
			}else if(ugByCode!=null){
				proxyResult.setErrorCode(217760472);//用户组Code已存在
				log.error("[ERR217760472] 此code获取的用户组已存在");
			}else{
				int id = sequenceProducer.nextval("t_ibcp_usergroup");
				group.setId(id);
				userGroupDao.addUserGroup(group);
				proxyResult.setErrorCode(0);//添加用户组成功
				proxyResult.setData(id);//返回id值
				log.info("[INF221722586] 添加用户组成功");
				
				// 添加审计追踪记录
				AuditTrailRecord atRecord = new AuditTrailRecord();
				atRecord.setSid(sid); // 签名 id
				atRecord.setUid(iuid); // 用户 id
				atRecord.setLid(100);
				atRecord.setTid(AuditTrailType.AddUserGroup.GetId()); // 审计追踪类型 id
				atRecord.setOtid(AuditTrailObject.UserGroup.getId());    // 审计追踪对象 id

				Document metadata = new Document(); // 审计追踪元数据
				metadata.append("UserGroupId", group.getId());
				metadata.append("UserGroupName",  group.getName());
				atRecord.setMeta(metadata);
				auditTrailSvc.addRecord(atRecord);
			}//else 语句结束
			return proxyResult;	
		}catch(Exception e){// try 语句结束
			proxyResult.setErrorCode(218085311);
			log.error("[ERR218085311] 添加用户组失败" + e.getMessage() );  
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}	
	}

	/**
	 * <p>Function: updateUserGroup</p>
	 * <p>Description: 更新UserGroup表信息</p>
	 * @author wan shuaishuai
	 * @param  userGroup	 group	User Group Object
	 * @param   ErrorCode  错误代码
	 * @param  	int sid 签名 id（操作前用户确认签名记录的 id）
	 * @param   Data       用户组数据
	 * @return proxyResult	      返回结果封装了Data和ErrorCode 
	 */
	@Override
	public ServiceResult<Integer> updateUserGroup(UserGroup group,int sid ,int iuid) {
		ServiceResult<Integer> proxyResult = new ServiceResult<Integer>();
		try{
			String name = group.getName();
			UserGroup ugByName = userGroupDao.getUserGroupByName(name);
			System.out.println(ugByName);
			System.out.println(group.getId());
			if(ugByName!=null&&(ugByName.getId()!=group.getId())){
				proxyResult.setErrorCode(217760578);//用户组名已存在
				log.error("[ERR217760578] 此name获取的用户组已存在");
			}else{
				userGroupDao.updateUserGroup(group);
				proxyResult.setErrorCode(0);//用户组更新成功
				log.info("[INF221722617] 更新用户组成功");
				
				// 添加审计追踪记录
				AuditTrailRecord atRecord = new AuditTrailRecord();
				atRecord.setSid(sid); // 签名 id
				atRecord.setUid(iuid); // 用户 id
				atRecord.setLid(100);
				atRecord.setTid(AuditTrailType.UpdateUserGroup.GetId()); // 审计追踪类型 id
				atRecord.setOtid(AuditTrailObject.UserGroup.getId());    // 审计追踪对象 id

				Document metadata = new Document(); // 审计追踪元数据
				System.out.println(metadata);
				metadata.append("UserGroupId", group.getId());
				metadata.append("OldUserGroupName", name);//变更前的用户组名
				metadata.append("UserGroupName",  group.getName());//变更后的用户组名
				atRecord.setMeta(metadata);
				
				auditTrailSvc.addRecord(atRecord);
			}//else 语句结束
			return proxyResult;
		}catch(Exception e){//try 语句结束
			proxyResult.setErrorCode(218085320);
			log.error("[ERR218085320] 更新用户组失败" + e.getMessage() ); 
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}	
	}
	
	/**
	 * <p>Function: InvalidateUserGroup</p>
	 * <p>Description: 更新无效用户状态</p>
	 * @author wan shuaishuai
	 * @param  uid	 User Group Id
	 * @param  	int sid 签名 id（操作前用户确认签名记录的 id）
	 * @param   ErrorCode  错误代码
	 * @param   Data       用户组数据
	 * @param   val        更新状态值(成功:1,失败:0 )
	 * @return proxyResult	      返回结果封装了Data和ErrorCode 
	 */
	@Override
	public ServiceResult<Integer> invalidateUserGroup(int ugid, int sid, int iuid) {
		ServiceResult<Integer> result = new ServiceResult<Integer>();
		try{
			UserGroup userGroup = userGroupDao.getUserGroupById(ugid);
			//查询此用户组的权限
			List<Authority> authority = authorityDao.queryAuthoritys(ugid);
			int status = userGroup.getStatus();
			int lock = status | 16;// 按位或操作，16对应00010000，第四位为5
			userGroup.setStatus(lock);
			userGroupDao.invalidateUserGroup(userGroup);
			result.setErrorCode(0);//设置无效用户组成功
			log.info("[INF221722661] 更新无效用户组状态成功");
			
			authorityDao.deleteUserGroupAuthorityByUgid(ugid);//删除此用组的权限
			// 添加审计追踪记录
			AuditTrailRecord atRecord = new AuditTrailRecord();
			atRecord.setSid(sid); // 签名 id
			atRecord.setUid(iuid); // 用户 id
			atRecord.setLid(100);
			atRecord.setTid(AuditTrailType.InvalidateUserGroup.GetId()); // 审计追踪类型 id
			atRecord.setOtid(AuditTrailObject.UserGroup.getId());    // 审计追踪对象 id
			
			Document metadata = new Document(); // 审计追踪元数据
			metadata.append("UserGroupId", userGroup.getId());
			metadata.append("Authority", authority);//用户组变更前的权限
			metadata.append("UserGroupName", userGroup.getName());
			atRecord.setMeta(metadata);
			
			auditTrailSvc.addRecord(atRecord);
			return result;	
		}catch(Exception e){//try语句结束
			result.setErrorCode(218085251);
			log.error("[ERR218085251] 更改无效用户组状态失败" + e.getMessage() );
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return result;
		}	
	}

	/**
	 * <p>Function: FindUserGroup</p>
	 * <p>Description: 通过code，name，status查询用户组</p>
	 * @author wan shuaishuai
	 * @param  code	  代码    
	 * @param  name  用户组名
	 * @param  status  状态值
	 * @param  iuid  调用者id
	 * @param   ErrorCode  错误代码
	 * @param   Data       用户组数据
	 * @return proxyResult	      返回结果封装了Data和ErrorCode 
	 */
	@Override
	public ServiceResult<List<Integer>> findUserGroup(String code, String name, UserGroupStatus[] status, int iuid) {
		// code like %#{code}% and name like %#{name}% and (status&16)>0 and (status&16)=0
		// -- status&16 > 0  判断是否失效
		// -- status&16 = 0 判断是否未失效
		ServiceResult<List<Integer>> proxyResult = new ServiceResult<List<Integer>>();
		try{
			String sql = "select id from t_ibcp_usergroup";
			if((code==null || code=="") && (name==null || name=="") && (status==null || status.length==0)){
				sql = "select id from t_ibcp_usergroup";
			}else{
				sql +=" where";
				//状态值不为空
				if (status != null && status.length != 0) {
					for (int i = 0; i < status.length; i++) {
						switch (status[i]) {
						case Invalid:
							sql += " (status&16)>0";
							break;
						case Valid:
							sql += " (status&16)=0";
							break;
						default:
							proxyResult.setErrorCode(221707165);
							log.error("[ERR221707165] 状态错误");
							return proxyResult;
						}//switch语句结束
					}//for循环语句结束
				}//eles内if语句结束
			}//else语句结束
			Map<String,String> map = new HashMap<String,String>();
			System.out.println(sql);
			map.put("sql", sql);
			List<Integer> id = userGroupDao.findUserGroup(map);
			proxyResult.setData(id);
			proxyResult.setErrorCode(0);
			log.info("[INF221722753] 通过用户组name，code，status查询用户组成功");
			return proxyResult;	
		}catch(Exception e){//try 语句结束
			proxyResult.setErrorCode(220514282);
			log.error("[ERR220514282] 查询用户组失败" + e.getMessage() );  
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}
	}

	/**
	 * <p>Function: findUser</p>
	 * <p>Description: 通过用户组id查询用户组</p>
	 * @author wan shuaishuai
	 * @param  ugid	   用户组id   
	 * @param   ErrorCode  错误代码
	 * @param   Data       用户数据
	 * @return proxyResult	      返回结果封装了Data和ErrorCode 
	 */
	@Override
	public ServiceResult<List<Integer>> findUser(int ugid,int iuid) {
		ServiceResult<List<Integer>> serviceResult = new ServiceResult<List<Integer>>();
		try{
			List<Integer> set=userGroupDao.findUser(ugid);
			serviceResult.setData(set);
			serviceResult.setErrorCode(0);
			log.info("[INF221722799] 查询用户成功");
			return serviceResult;
		}catch(Exception e){
			serviceResult.setErrorCode(220692788);
			log.error("[ERR220692788] 查询用户组失败" + e.getMessage() );
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return serviceResult;		
		
		}
	}
	/**
	 * <p>Function: findUser</p>
	 * <p>Description: 通过用户组id查询用户组</p>
	 * @author wan shuaishuai
	 * @param  ugid	   用户组id   
	 * @param   ErrorCode  错误代码
	 * @param   Data       用户数据
	 * @return proxyResult	      返回结果封装了Data和ErrorCode 
	 */
	@Override
	public ServiceResult<List<User>> findUsers(int ugid,int iuid) {
		// TODO Auto-generated method stub
		ServiceResult<List<User>> serviceResult = new ServiceResult<List<User>>();
		try{
			List<User> list=userGroupDao.findUsers(ugid);
			serviceResult.setData(list);
			serviceResult.setErrorCode(0);
			log.info("[INF221722799] 查询用户成功");
			return serviceResult;
		}catch(Exception e){
			serviceResult.setErrorCode(223540627);
			log.error("[ERR223540627] 查询用户组失败" + e.getMessage() ); 
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return serviceResult;		
		
		}
	}
	/**
	 * <p>Function: FindUserGroups</p>
	 * <p>Description: 通过code，name，status查询用户组</p>
	 * @author Huang Zhong wei
	 * @param  code	  代码    
	 * @param  name  用户组名
	 * @param  status  状态值
	 * @param  iuid  调用者id
	 * @param  ErrorCode  错误代码
	 * @param  Data       用户组数据
	 * @return result	      返回结果封装了Data和ErrorCode 
	 */
	@Override
	public ServiceResult<List<UserGroup>> findUserGroups(String code, String name, UserGroupStatus[] status, int iuid) {
		ServiceResult<List<UserGroup>> result = new ServiceResult<List<UserGroup>>();
		try{
			String sql = "select t1.id,t1.code,t1.name,t1.description,t1.status,t2.describe statusDescription "+
					"FROM t_ibcp_usergroup t1 "+
					"LEFT JOIN t_ibcp_usrergroup_status t2 "+ 
					"ON t1.status = t2.id ";
			if((code==null ||"".equals(code)) && (name==null || "".equals(name)) && (status==null || status.length==0)){
				//do nothing
			}else{
				sql +=" where";
				if(code==null){
					sql += " code like '%%'";
				}else{
					sql	+="  code like '%" + code+"%'";
				}
				if(name!=null){
					sql += " and name like '%" + name+"%'";
				}else{
					sql += " and name like '%%'";
				}	
				if (status != null && status.length != 0) {
					for (int i = 0; i < status.length; i++) {
						switch (status[i]) {
						case Invalid:
							sql += " and (status&16)>0";
							break;
						case Valid:
							sql += " and (status&16)=0";
							break;
						default:
							result.setErrorCode(221723615);
							log.error("[ERR221723615] 状态错误");
							return result;
							}//switch语句结束
						}//for循环结束
					}	//外层else中if语句结束	
				}//外层else语句结束
			Map<String,String> map = new HashMap<String,String>();
			System.out.println(sql);
			map.put("sql", sql);
			List<UserGroup> userGroups = userGroupDao.findUserGroups(map);
			result.setData(userGroups);
			result.setErrorCode(0);
			log.info("[INF221809295] 查询用户组成功");
			return result;	
		}catch(Exception e){//try 语句结束
			result.setErrorCode(220514282);
			log.error("[ERR220514282] 查询用户组失败" + e.getMessage() );  
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return result;
		}
	}	
	/**
	 * 在用户组中添加用户
	 * @param ugid  用户组 id
	 * @param uid   用户 id
	 * @param  	int sid 签名 id（操作前用户确认签名记录的 id）
	 * @param iuid  调用者 id
	 * @return	ErrorCode
	 */
	@Override
	public ServiceResult<Integer> addMember(int ugid, List<Integer> uidList,int sid, int iuid) {
		// t_ibcp_usergroup_member 表
		// 检查用户是否已存在用户组中
		// 若存在，直接返回成功
		// 若不存在，将用户添加至用户组，并增加审计追踪记录	
		ServiceResult<Integer> proxyResult = new ServiceResult<Integer>();
		try {
			Map<String,Integer> map=new HashMap<String,Integer>();
			Document metadata = new Document(); // 审计追踪元数据
			for(int i:uidList){
				int uid=i;
				map.put("uid",uid);
				map.put("ugid", ugid);
				Map<String,Integer> m=userGroupDao.findMember(map);
				if (m == null) {
					String oldUgName;
					List<Integer> oldUserGroupId = userDao.getUserGroup(uid);
					metadata.append("UserGroupId", oldUserGroupId);//变更用户组前用户组id
					for(int id : oldUserGroupId){
						UserGroup ug = userGroupDao.getUserGroupById(id);
						oldUgName = ug.getName(); 
						metadata.append("UserGroupName",oldUgName);//变更用户组前用户组名
					}
					userGroupDao.addMember(map);
					log.info("[INF221816243] addMember成功！");
					
					// 添加审计追踪记录
					AuditTrailRecord atRecord = new AuditTrailRecord();
					atRecord.setSid(sid); // 签名 id
					atRecord.setUid(iuid); // 用户 id
					atRecord.setLid(100);
					atRecord.setTid(AuditTrailType.AddMember.GetId()); // 审计追踪类型 id
					atRecord.setOtid(AuditTrailObject.UserGroup.getId());    // 审计追踪对象 id
					
					List<Integer> newUserGroupId = userDao.getUserGroup(uid);//执行添加后用户所在的用户组id集合
					
					for(int id : newUserGroupId){
						UserGroup ug = userGroupDao.getUserGroupById(id);
						String newUgName = ug.getName(); //执行添加用户后的用户所在用户组名
						metadata.append("newUerGroupId", id);//执行添加后用户所在的用户组id
						metadata.append("newUerGroupName", newUgName);//执行添加后用户所在的用户组名
					}
					User user = userDao.getUserById(uid);
					metadata.append("UserId", uid);//审计追踪记录中显示用户Id
					metadata.append("UserName", user.getName() );//审计追踪记录中显示用户名
					metadata.append("UserName", user.getAccount() );//审计追踪记录中显示用户账户
					
					
					atRecord.setMeta(metadata);
					auditTrailSvc.addRecord(atRecord);
					
				}else{//if语句结束
					log.info("[INF221816243] 此用户已经存在此用户组！");
				}
			}//外层for 循环结束
			proxyResult.setErrorCode(0);
			return proxyResult;
		} catch (Exception e) {//try 语句结束
			proxyResult.setErrorCode(221816246);
			log.error("[ERR221816246] addMember失败", ExceptionUtils.getFullStackTrace(e));
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}
	}
	
	/**
	 * 在用户组中删除用户
	 * @param ugid  用户组 id
	 * @param uid   用户 id
	 * @param  	int sid 签名 id（操作前用户确认签名记录的 id）
	 * @param iuid  调用者 id
	 * @return	ErrorCode
	 */
	@Override
	public ServiceResult<Integer> deleteMember(int ugid, List<Integer> uidList,int sid, int iuid) {
		ServiceResult<Integer> proxyResult = new ServiceResult<Integer>();
		try {
			Map<String,Integer> map=new HashMap<String,Integer>();
			Document metadata = new Document(); // 审计追踪元数据
			for(int i:uidList){//遍历uidList,获取uid
				int uid=i;
				map.put("uid",uid);
				map.put("ugid", ugid);
				Map<String,Integer> m=userGroupDao.findMember(map);
				if (m == null) {
					proxyResult.setErrorCode(221884233);//此用户组没有此成员
					log.error("[ERR221884233] 此用户组没有此成员");
				}else{
					String oldUgName;
					List<Integer> oldUserGroupId = userDao.getUserGroup(uid);
					metadata.append("UserGroupId", oldUserGroupId);//变更用户组前用户组id
					for(int id : oldUserGroupId){
						UserGroup ug = userGroupDao.getUserGroupById(id);
						oldUgName = ug.getName(); 
						metadata.append("UserGroupName",oldUgName);//变更用户组前用户组名
					}
					userGroupDao.deleteMember(map);
					proxyResult.setErrorCode(0);
					log.info("[INF221884277] deleteMember成功！");
					
					// 添加审计追踪记录
					AuditTrailRecord atRecord = new AuditTrailRecord();
					atRecord.setSid(sid); // 签名 id
					atRecord.setUid(iuid); // 用户 id
					atRecord.setLid(100);
					atRecord.setTid(AuditTrailType.AddMember.GetId()); // 审计追踪类型 id
					atRecord.setOtid(AuditTrailObject.UserGroup.getId());    // 审计追踪对象 id
					List<Integer> newUserGroupId = userDao.getUserGroup(uid);//执行添加后用户所在的用户组id集合
					
					for(int id : newUserGroupId){
						UserGroup ug = userGroupDao.getUserGroupById(id);
						String newUgName = ug.getName(); //执行删除用户后的用户所在用户组名
						metadata.append("newUerGroupId", id);//执行添加后用户所在的用户组id
						metadata.append("newUerGroupName", newUgName);//执行添加后用户所在的用户组名
					}
					User user = userDao.getUserById(uid);
					metadata.append("UserId", uid);//审计追踪记录中显示用户Id
					metadata.append("UserName", user.getName() );//审计追踪记录中显示用户名
					metadata.append("UserName", user.getAccount() );//审计追踪记录中显示用户账户

					atRecord.setMeta(metadata);
					auditTrailSvc.addRecord(atRecord);
				}//else 语句结束
			}//外层for循环 语句结束
			return proxyResult;
		} catch (Exception e) {//try 语句 结束
			proxyResult.setErrorCode(221884234);
			log.error("[ERR221884234] deleteMember失败", ExceptionUtils.getFullStackTrace(e));
			TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();
			return proxyResult;
		}
	}
}
